import { observable, action, computed } from 'mobx';
import { message } from 'antd';
import { WeaTableNew } from 'comsMobx';
import * as API from '../../apis/performance/accessAuditListApi'
import { idsAndNamesToArr } from '../../util/Util'
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-待考核评分及审批列表
 * jiansong 2019/6/11
 */
const { TableStore } = WeaTableNew;
export default class AccessAuditListStore {

    @observable loading = true; //加载中
    @observable tableStore = new TableStore(); // new table
    @observable showSearchAd = false; // 高级搜索面板显示

    //考核方案未设置提醒数量
    @observable amount1 = 0; //年度
    @observable amount2 = 0; //半年
    @observable amount3 = 0; //季度
    @observable amount4 = 0; //月度

    @computed get hrmData() { //人力资源回显数据
        return idsAndNamesToArr(this.queryParams.hrmids, this.queryParams.hrmnames);
    };
    @computed get subcompanyData() { //分部回显数据
        return idsAndNamesToArr(this.queryParams.subcompanyids, this.queryParams.subcompanynames);
    };
    @computed get departmentData() { //部门回显数据
        return idsAndNamesToArr(this.queryParams.departmentids, this.queryParams.departmentnames);
    };

    //类型data
    @observable typeData = [
        { viewcondition: "1", title: getLabel('6106', '考核'),showcount: true,groupid: "accessscore",color:"rgb(0, 0, 0)" },
        { viewcondition: "2", title: getLabel('30835', '审批'),showcount: true,groupid: "auditscore",color:"rgb(0, 0, 0)" },
    ];
    //周期
    @observable showstatusData = [
        { key: "", showname: "" },
        { key: "0", showname: getLabel(508778, '考核中') },
        { key: "1", showname: getLabel('19134', '审批中') },
        { key: "2", showname: getLabel('236', '退回') },
        { key: "3", showname: getLabel(23774, '已完成') },
    ];
    //周期
    @observable cicleData = [
        { key: "", showname: "" },
        { key: "1", showname: getLabel('19398', '月度') },
        { key: "2", showname: getLabel('17495', '季度') },
        { key: "3", showname: getLabel('20729', '半年') },
        { key: "4", showname: getLabel('17138', '年度') },
    ];
    @observable includesubData = [
        { key: "3", showname: getLabel('508780', '含子分部') },
        { key: "1", showname: getLabel('18919', '仅本分部') },
        { key: "2", showname: getLabel('18920', '仅子分部') },
    ];
    @observable includedeptData = [
        { key: "3", showname: getLabel('508784', '含子部门') },
        { key: "1", showname: getLabel('18916', '仅本部门') },
        { key: "2", showname: getLabel('18917', '仅子部门') },
    ];

    @observable queryParams = {  //查询条件
        scorename: '', //标题
        showtype: '', //类型
        showstatus: '', //考核状态，只有在考核选项卡中才有
        type1: null, //周期
        hrmids: null, //人员
        hrmnames: null, //人员名称
        includesub: "3", //是否包含分部，默认含子分部
        subcompanyids: null, //分部
        subcompanynames: null, //分部名称
        includedept: "3", //是否包含分部，默认含子部门
        departmentids: null, //部门
        departmentnames: null, //部门名称
    };
    @observable newcount = [];

    @action("初始化操作")
    doInit = () => {
        this.queryParams = {  //查询条件
            scorename: '', //标题
            showtype: this.queryParams.showtype, //类型
            showstatus: null, //考核状态，只有在考核选项卡中才有
            type1: this.queryParams.type1, //周期
            hrmids: null, //人员
            hrmnames: null, //人员名称
            includesub: "3", //是否包含分部，默认含子分部
            subcompanyids: null, //分部
            subcompanynames: null, //分部名称
            includedept: "3", //是否包含分部，默认含子部门
            departmentids: null, //部门
            departmentnames: null, //部门名称
        };
        this.getAccessAuditListData();
        this.getAccessAuditProgramNumData();
    };

    @action("考核方案未设置数量")
    getAccessAuditProgramNumData = (params) => {
        this.loading = true;
        API.accessAuditProgramNumApi(params).then(action(res => {
            if (res.api_status) {
                this.amount1 = res.amount1;
                this.amount2 = res.amount2;
                this.amount3 = res.amount3;
                this.amount4 = res.amount4;
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    };

    @action("高级搜索框")
    setShowSearchAd = bool => this.showSearchAd = bool;

    @action("搜索条件改变")
    onSearchChange = (params) => {
        for (let key in params) {
            this.queryParams[key] = params[key];
        }
    }

    @action("搜索")
    doSearch = () => {
        this.showSearchAd = false;
        this.getAccessAuditListData();
    };

    @action("重置查询条件")
    resetSearchAd = () => {
        this.queryParams.scorename = ''; //标题
        this.queryParams.type1 = null; //周期
        this.queryParams.hrmids = null; //人员
        this.queryParams.showstatus = null; //考核状态
        this.queryParams.hrmnames = null; //人员名称
        this.queryParams.includesub = "3"; //是否包含分部，默认含子分部
        this.queryParams.subcompanyids = null; //分部
        this.queryParams.subcompanynames = null; //分部名称
        this.queryParams.includedept = "3"; //是否包含分部，默认含子部门
        this.queryParams.departmentids = null; //部门
        this.queryParams.departmentnames = null; //部门名称
    };

    @action("待考核评分及审批列表")
    getAccessAuditListData = () => {
        this.loading = true;
        API.accessAuditListApi(this.queryParams).then(action(res => {
            if (res.api_status) {
                this.tableStore.getDatas(res.datas, 1,"", []);
                this.getAccessCount();
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    };
    @action("待考核评分操作-非全部")
    getAccessCount = () => {
        let params = {type1:this.queryParams.type1,operation:"getCount"};
        API.accessAuditOperationApi(params).then(action(res => {
            if (res.api_status) {
                if(res.scoremap){
                    this.newcount = res.scoremap;
                }else{
                    this.newcount = [];
                }
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
        }));
    };

    @action("待考核评分操作-非全部")
    accessAuditOperation = (params, updateNum) => {
        this.loading = true;
        API.accessAuditOperationApi(params).then(action(res => {
            if (res.api_status) {
                if (!res.hasRight) {
                    message.error(getLabel('2012', '对不起，您暂时没有权限！'));
                } else {
                    this.tableStore.getDatas(res.datas);
                    updateNum();
                    this.getAccessCount();
                    message.success(getLabel('30700', '操作成功'));
                }
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    };

    @action("待考核评分操作-全部")
    accessAuditOperationAll = (operation, updateNum) => {
        this.loading = true;
        let params = {
            ...this.queryParams,
            operation: operation,
        };
        API.accessAuditOperationApi(params).then(action(res => {
            if (res.api_status) {
                if (!res.hasRight) {
                    message.error(getLabel('2012', '对不起，您暂时没有权限！'));
                } else if (res.count == 0) {
                    message.warning(getLabel('508822', '当前查询条件不存在可以操作的考核结果'));
                } else {
                    this.tableStore.getDatas(res.datas);
                    updateNum();
                    this.getAccessCount();
                    message.success(getLabel('30700', '操作成功'));
                }
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    };

}